#region File Description
//  DomoBCI 
//  License: GNU/GPL
//  Developer: Federico Leonardo Motta
//  File description: Handler for the internal stimulator.
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#endregion

namespace DomoBCI.Core.Stimulation.Internal
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class StimulationBase : Microsoft.Xna.Framework.DrawableGameComponent
    {
        float t0, t1, t2, t3;
        public Texture2D LeftSq, RightSq, UpSq, DownSq;
        public Boolean left, right, up, down;
        Texture2D stim1, stim2;
        private float[] Frequencies = new float[4];
        SpriteBatch sprite_batch;
        Boolean isPaused = true;

        public StimulationBase(Game game)
            : base(game)
        {
            left = true; right = true; up = true; down = true;
        }

       
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            int t = gameTime.ElapsedGameTime.Milliseconds;
            t0 += t;
            t1 += t;
            t2 += t;
            t3 += t;

            if (t0 >= ((Frequencies[0]) / 256) * 1000)
            {
                t0 = 0;
                if (LeftSq == Stim1)
                    LeftSq = Stim2;
                else
                    LeftSq = Stim1;
            }
            if (t1 >= ((Frequencies[1]) / 256) * 1000)
            {
                t1 = 0;
                if (RightSq == Stim1)
                    RightSq = Stim2;
                else
                    RightSq = Stim1;
            }
            if (t2 >= ((Frequencies[2]) / 256) * 1000)
            {
                t2 = 0;
                if (UpSq == Stim1)
                    UpSq = Stim2;
                else
                    UpSq = Stim1;
            }
            if (t3 >= ((Frequencies[3]) / 256) * 1000)
            {
                t3 = 0;
                if (DownSq == Stim1)
                    DownSq = Stim2;
                else
                    DownSq = Stim1;
            }

            base.Update(gameTime);
        }

        public Texture2D Stim1 {
            set { stim1 = value; }
            get { return stim1; }
        }

        public Texture2D Stim2
        {
            set { stim2 = value; }
            get { return stim2; }
        }

        public SpriteBatch spriteBatch
        {
            set { sprite_batch = value; }
            get { return sprite_batch; }
        }

        public void SetFrequencies(int l, int r, int u, int d)
        {
            //5,9 = 43 //6,9 = 37 //8 = 32 //9,14 = 28
            //9,8 = 26 //11,13 = 23 //12,19 = 21 //12,8 = 20
            //14,22 = 18 //15,05 = 17 //16 = 16 //17 = 15
            if(l==9999){ left = false;}else{left = true;}
            if(r==9999){ right = false;}else{right = true;}
            if(u==9999){ up = false;}else{up = true;}
            if(d==9999){ down = false;}else{down = true;}
            Frequencies[0] = l;
            Frequencies[1] = r;
            Frequencies[2] = u;
            Frequencies[3] = d;
        }

        public void Pause() {
            isPaused = true;
        }

        public void Start() {
            isPaused = false;
        }

        public Boolean IsPaused{
            get { return isPaused; }
        }
    }
}
